const fs=require("fs");
const path=require("path");
const inquirer = require('inquirer');
const paths=require("../config/paths")
const {readFile}=require("./util")
const cli=async ()=>{
    const answers=await inquirer.prompt([{
        type: 'input',
        name: 'name',
        message: '请输入文件名',
        require:true
    }]);
    const {name}=answers;
    const filePath=path.join(paths.components,name);
    const examplePath=path.join(paths.examples,name);
    try {
        const file=await fs.lstatSync(filePath);
        const example=await fs.lstatSync(examplePath);
        if (file.isDirectory()){
            console.log("components目录已存在请删除后重试!")
        }
        if (example.isDirectory()){
            console.log("examples目录已存在请删除后重试!")
        }
    }catch (e){
        await fs.mkdirSync(filePath);
        await fs.mkdirSync(examplePath);
        await fs.openSync(path.join(filePath,"/index.tsx"),"w");
        await fs.openSync(path.join(examplePath,"/index.tsx"),"w");
        const packageJson=await readFile(paths.temPackageJson);
        packageJson.name=packageJson.name.replace("${name}",name).toLowerCase();
        const answers=await inquirer.prompt([{
            type: 'input',
            name: 'description',
            message: '请输入description',
            default:"教务课程前端组出品"
        },{
            type: 'input',
            name: 'version',
            message: '请输入version',
            default:"0.0.0"
        }]);
        const {description,version}=answers;
        packageJson.description=description;
        packageJson.version=version;
        await fs.writeFileSync(path.join(filePath,"./package.json"),JSON.stringify(packageJson,"","\t"));
        console.log("创建成功!")
    }
}

cli();